async function btnClickListener(event, dom) {
    if (dom.pluginName.value.length <= 0) {
        // const myDialog = document.getElementById('dialog')
        // myDialog.innerHTML = "<p>插件名称未设置</p>"
        return;
    }
    if (dom.serverName.value.length <= 0) {
        // const myDialog = document.getElementById('dialog')
        // myDialog.innerHTML = "<p>插件名称未设置</p>"
        return;
    }

    await session.remove('TOKEN_RUNNING')
    await session.set('pluginName', dom.pluginName.value)
    await session.set('serverName', dom.serverName.value)
    await session.set('hostCode', 'msc')

    //TODO 每个网站需要设置
    let domain = 'msccargo.cn'
    await session.set('domain', domain)

    chrome.tabs.query({}, function (tabs) {
        for (let i = 0; i < tabs.length; i++) {
            const tab = tabs[i]
            if (tab.url.indexOf(domain) != -1) {
                //给myscript.js发送通知
                chrome.tabs.sendMessage(tab.id, {type: 'init'}, function (response) {
                    console.log('开始准备参数')
                });
            }
        }
    });
}

async function setValue() {
    const pluginName = document.getElementById('pluginName')
    const serverName = document.getElementById('serverName')
    const saveBtn = document.getElementById('saveBtn')

    let res = await session.get_objects(null);

    initInput(pluginName, res["pluginName"])
    initInput(serverName, res["serverName"])

    const dom = {
        "pluginName": pluginName,
        "serverName": serverName
    }

    saveBtn.addEventListener('click', event => {
        btnClickListener(event, dom)
    })

}

function initInput(ele, value) {
    if (value && value.length > 0) {
        ele.value = value
    }
}

async function loaded() {
    await setValue();
    return true;
}

document.addEventListener('DOMContentLoaded', loaded)